<template>
  <van-popup
    :value="value"
    closeable
    position="left"
    @click-close-icon="$emit('input', false)"
  >
    <div class="article-channel">
      <div class="head">
        <h3>我的频道<small>点击进入频道</small></h3>
        <a class="edit" href="javascript:;" @click="isEdit=!isEdit" :class="{active:isEdit}">{{isEdit?'完成':'编辑'}}</a>
      </div>

      <div class="body">
        <!-- <a href="javascript:;" class="active">推荐</a> -->
        <a href="javascript:;" @click="enterChannel(index)" :class="{active:activeIndex===index}" v-for="(channel,index) in mychannels" :key="channel.id">{{ channel.name }}
            <geek-icon @click.stop.native="deleteChannel(channel.id, index)" name="tag-close" v-show="isEdit && index!==0 && activeIndex !== index"></geek-icon>
        </a>
      </div>

      <div class="head" style="margin-top: 12px">
        <h3>频道推荐</h3>
      </div>

      <div class="body">
        <a href="javascript:;" @click="addChannel(channel.id, channel.name)" v-for="channel in optionChannels" :key="channel.id">+ {{ channel.name }}</a>
      </div>
    </div>
  </van-popup>
</template>
<script>
import { getAllChannels } from '@/api/channel'
import { addChannel, deleteChannel } from '@/api/user'
export default {
  // 1.props 接收的时候 有两种形式 - 数组  、对象
  //   props: ['value'],
  props: ['value', 'mychannels', 'activeIndex'],
  data () {
    return {
      show: false,
      allChannelList: [],
      isEdit: false
    }
  },
  created () {
    this.getAllChannels()
  },
  computed: {
    optionChannels () {
      return this.allChannelList.filter((item) => {
        return !this.mychannels.find((c) => c.id === item.id)
      })
    }
  },
  methods: {
    async deleteChannel (id, index) {
      await deleteChannel(id)
      //   if (err) this.$toast.fail(err)
      //   console.log(data)
      //   this.mychannels = this.mychannels.filter((item) => {
      //     return item.id !== id
      //   })

      this.mychannels.splice(index, 1)
    },
    async addChannel (id, name) {
      // 1.准备请求的数据
    //   const list = []
    //   //   先将已有的数据拼接到list中去
    //   this.mychannels.forEach((item, index) => {
    //     list.push({
    //       id: item.id,
    //       seq: index + 1
    //     })
    //   })
    //   //   将点击的数据加入list中
    //   list.push({
    //     id,
    //     seq: list.length + 1
    //   })
    //   //   调用api方法
    // 1。将全部数据传递给数据层
      const list = [...this.mychannels, { id, name }]
      console.log(7777)
      await addChannel(list)
      console.log(888)
      //   if (err) this.$toast.fail(err)
      //   //   console.log(data)

      this.mychannels.push({
        id,
        name
      })
    },
    enterChannel (index) {
      //   1.关闭van-popup
      this.$emit('input', false)

      this.$emit('update:activeIndex', index)
    },
    async getAllChannels () {
      const [err, data] = await getAllChannels()
      if (err) this.$toast.fail(err.message)
      //   console.log(data)
      // 给allChannelList赋值
      this.allChannelList = data.data.channels
    }
  }
}
</script>
<style scoped lang="less">
.van-popup {
  width: 100%;
  height: 100%;
  ::v-deep .van-popup__close-icon {
    font-size: 20px;
    right: 12px;
    top: 12px;
  }
}
.article-channel {
  margin-top: 44px;
  .head {
    padding: 0 16px;
    display: flex;
    justify-content: space-between;
    justify-items: center;
    padding-bottom: 12px;
    h3 {
      font-size: 16px;
      color: #333;
      margin: 0;
      small {
        font-size: 12px;
        color: #999;
        margin-left: 10px;
      }
    }
    .edit {
      float: right;
      height: 22px;
      width: 52px;
      line-height: 22px;
      text-align: center;
      color: #DE644B;
      border-radius: 11px;
      border: 1px solid #DE644B;
      font-size: 12px;
      &.active {
        color: #fff;
        background: #DE644B;
      }
    }
  }
  .body {
    padding: 4px 6px 0 16px;
    a {
        position: relative;
      display: inline-block;
      padding: 0 8px;
      font-size:14px;
      color: #3A3948;
      background: #F7F8FA;
      height: 36px;
      line-height: 36px;
      min-width: 78px;
      margin-right: 10px;
      margin-bottom: 12px;
      border-radius: 18px;
      text-align: center;
      &.active {
        color: @geek-color;
      }
      .geek-icon {
        position: absolute;
        top: -5px;
        right: -5px;
        line-height: 1;
      }
    }
  }
}
</style>
